package com.xh.lesson.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.lesson.entity.SysFun;
import com.xh.lesson.exception.BusinessException;
import com.xh.lesson.exception.code.BaseResponseCode;
import com.xh.lesson.mapper.SysFundMapper;
import com.xh.lesson.service.SysFunService;
import com.xh.lesson.vo.req.FunAddReqVO;
import com.xh.lesson.vo.req.FunUpdateReqVO;


import com.xh.lesson.vo.req.HomeAddReqVO;
import com.xh.lesson.vo.resp.DepartChart;
import com.xh.lesson.vo.resp.EnterpriseChart;
import com.xh.lesson.vo.resp.FunChart;
import com.xh.lesson.vo.resp.FunUseChart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 柴&许
 * @since 2021-07-01
 */
@Service
@Slf4j
public class SysFunServiceImpl extends ServiceImpl<SysFundMapper, SysFun> implements SysFunService {

    @Override
    public SysFun selectFunById(Integer id) {
        SysFun sysFun = baseMapper.selectById(id);
        if (null==sysFun){
            log.error("传入 的 id:{}不合法",id);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        return sysFun;
    }

    @Override
    public void edit(FunUpdateReqVO vo) {
        SysFun sysFun = baseMapper.selectById(vo.getFunid());
        if (null==sysFun){
            log.error("传入 的 id:{}不合法",vo.getFunid());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        System.out.println("需要修改的信息" +sysFun);
        BeanUtils.copyProperties(vo,sysFun);
        int count = baseMapper.updateById(sysFun);
        if (count!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        System.out.println("修改成功"+count);
    }

    @Override
    public SysFun addFun(HomeAddReqVO vo) {
        SysFun sysFun = new SysFun();
        BeanUtils.copyProperties(vo,sysFun);
        int insert = baseMapper.insert(sysFun);
        if (insert!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        return sysFun;
    }

    @Override
    public SysFun soloAddFun(FunAddReqVO vo) {
        SysFun sysFun = new SysFun();
        BeanUtils.copyProperties(vo,sysFun);
        int insert = baseMapper.insert(sysFun);
        if (insert!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        return sysFun;
    }

    @Override
    public void deleteFun(Integer fid) {
        int delete = baseMapper.deleteById(fid);
        if (delete!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }


    @Override
    public List<FunChart> selectFunChart() {
        List<FunChart> charts = baseMapper.selectFunChart();
        return charts;
    }

    @Override
    public List<FunUseChart> selectFunUseChart() {
        List<FunUseChart> charts = baseMapper.selectFunUseChart();
        return charts;
    }

    @Override
    public List<DepartChart> selectDepartChart() {
        List<DepartChart> departCharts = baseMapper.selectDepartChart();
        return departCharts;
    }

    @Override
    public List<EnterpriseChart> selectEnterpriseChart() {
        List<EnterpriseChart> charts = baseMapper.selectEnterpriseChart();
        return charts;
    }
}
